package dav.mobile.garuda.states.game.objects.encounters;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

import dav.mobile.garuda.components.ResourceManager;
import dav.mobile.garuda.components.SoundManager;
import dav.mobile.garuda.states.game.objects.Garuda;

public class Burung extends Encounter {
	//Outer bounds
	private float m_BoundWidth;
	private float m_BoundHeight;
	
	//Animation
	private Animation m_Animation;
	private float 	m_Time;
	
	//Attributes
	private float m_SpeedX;
	private float m_SpeedY;
	
	public Burung(int x, int y, int color, Vector2 force, float speed) {
		//Super
		super(x, y, color, force);
		
		//Generate texture and animation based on color
		m_Texture = ResourceManager.m_BurungSprite[color][0];
		
		m_Animation = ResourceManager.m_BurungAnimation[color];
		m_Time = 0.0f;
		
		//Set speed
		m_SpeedX = speed;
		m_SpeedY = 0.0f;
		
		//Get outer bounds
		m_BoundWidth = 0.5f * getWidth();
		m_BoundHeight = 0.5f * getHeight();
	}

	//Check outer bounds and implisitly act
	@Override
	public boolean DoesAABBOuterHit(Garuda garuda) {
		//Init
		boolean m_Hit = false;
		
		float xMin = getPosX() - m_BoundWidth;
		float yMin = getPosY() - m_BoundHeight;
		float xMax = getPosX() + (float) getWidth() + m_BoundWidth;
		float yMax = getPosY() + (float) getHeight() + m_BoundHeight;
		
		float garudaX = garuda.getPosX() + (float) garuda.getWidth();
		float garudaY = garuda.getPosY() + (float) garuda.getHeight();
		
		//Check collision
		m_Hit = !(xMin>garudaX||xMax<garuda.getPosX()||yMin>garudaY||yMax<garuda.getPosY());
		
		//Try to move to garuda
		if (m_Hit) { 
			m_SpeedY = (garuda.getPosY() - getPosY() > 0) ? 8.0f : -8.0f;
		}
		
		//Return
		return m_Hit;
	}

	@Override
	public void Update(float deltatime) {
		//Update time 
		m_Time += deltatime;
		
		//Move
		m_Position.x += deltatime * m_SpeedX;
		m_Position.y += deltatime * m_SpeedY;
	}
	
	@Override
	public void Render(SpriteBatch m_Spritebatch) {
		//Get keyframe
		m_Texture = m_Animation.getKeyFrame(m_Time, true);
		
		//Draw
		m_Spritebatch.draw(m_Texture, getPosX(), getPosY());	
	}

	@Override
	protected void PlaySound() {
		SoundManager.PlaySFX(SoundManager.m_BurungSound);
	}

}
